<?php

namespace backend\services\mall;

use backend\extensions\ApiBaseService;
use backend\models\mall\MallBanner;

/**
 * Desc 分类管理服务类
 * @author WMX
 */
class MallBannerService extends ApiBaseService
{

    /**
     * 列表
     * @param array $params 参数 
     * **/
    public static function getList($params)
    {
         //检索条件
         $condition = ['AND'];
         if ($params['name']) {
             $condition[] = ['like', 'name', $params['name']??''];
         }
         if ($params['state']) {
             $condition[] = ['=', 'state', $params['state']??''];
         }
         $list = MallBanner::getDataAll($condition, $params['page'], $params['page_size']);
         return $list;
    }

    /**
     * 新增
     * @param array $params 参数 
     * * */
    public static function create($params)
    {
        $params['stime'] = $params['time_range'][0];
        $params['etime'] = $params['time_range'][1];
        unset($params['time_range']);
        if (MallBanner::create($params) == false) {
            return self::jsonError('保存失败');
        }
        return self::jsonSuccess($params);
    }

    /**
     * 新增
     * @param array $params 参数 
     * * */
    public static function update($params)
    {
        $params['stime'] = $params['time_range'][0];
        $params['etime'] = $params['time_range'][1];
        unset($params['time_range']);
        $condition = ['id' => $params['id']];
        unset($params['id']);
        if (MallBanner::updateData($params, $condition) === false) {
            return self::jsonError('更新失败');
        }
        return self::jsonSuccess();
    }

    /**
     * 删除
     * @param array $params 参数 
     * * */
    public static function delete($params)
    {
        $condition = ['id' => $params['id']];
        if (MallBanner::del($condition) == false) {
            return self::jsonError('删除失败');
        }
        return self::jsonSuccess();
    }

    /**
     * 详情
     * @param array $params 参数 
     * **/
    public static function detail($params)
    {
        $condition = ['id' => $params['id']];
        $data = MallBanner::getDataOne($condition, '', ['id','name','img', 'state', 'sort', 'stime', 'etime', 'url']);
        $data['time_range'] = [$data['stime'],$data['etime']];
        return self::jsonSuccess($data);
    }

}
